﻿using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
 
using HZ.MBSM.DBUtility;
public partial class Deparment_Report_Reportlist : System.Web.UI.Page
{
    static int yy = 0, yytt = 0;
    protected void Page_Load(object sender, EventArgs e)
    {
         System.Collections.Generic.List<HZ.MBSM.DAL.AccountType> _AccountType = new System.Collections.Generic.List<HZ.MBSM.DAL.AccountType>();
        _AccountType.Add(HZ.MBSM.DAL.AccountType.enumDeparment);
        if (HZ.MBSM.BLL.Login.CheckAssert(_AccountType))
        {  
            HZ.MBSM.Model.Account _Account = (HZ.MBSM.Model.Account)Session["Account"];
            yy = 0; yytt = 0;
            if (!IsPostBack)
            {
                Label2.Text = DateTime.Now.ToLongDateString();
                Label1.Text = _Account.DWName + "日报表";
                SeletWithinInfo(0);  //();//各地市招标办反馈问题
                SeletWithinInfo(1);  //();//各地市医疗机构反馈问题
                SeleBuyPlanGoods(1); //();//送货中
                SeleBuyPlanGoods(2); //();//已入库
                SeleBuyPlanGoods(4); //();//无法配送
                SeleBuyPlanGoods(5); //();//到期未送
                SeleBuyPlanGoods(6); //();//货款到期未结算
                HosptialTotal();     //();//医院统计
               
            }
         }  
    }
    private void SeletWithinInfo(int i)//各地市机构反馈问题  0 招标办 1 医院
    {
        string strselt = "";
        if (i == 0)
        {
            strselt = "select count(WithinInfoid) as unm from WithinInfo where accountid<>6621 and datediff(dd,adddate,getdate())=0";
        }
        if (i == 1)
        {
            strselt = "select count(WithinInfoid) as num from WithinInfo_HA where datediff(dd,adddate,getdate())=0 and accountid not in(select accountid from Supervise )";
        }
        if (strselt != "")
        {
            Db db = new Db();
            DataTable dt = db.DataTable(strselt);
            db.Close();
            if (i == 0) Label9.Text = dt.Rows[0].ItemArray[0].ToString();
            if (i == 1) Label10.Text = dt.Rows[0].ItemArray[0].ToString();
        }
    }

    private void SeleBuyPlanGoods(int i)//采购情况  1 送货中    2 已入库    4 无法配送    5 到期未送  6 货款到期未结算
    { 
        string strsele = "";
        switch (i)
        { 
            case 1:
                strsele = "select count(distinct BuyPlanId) as num from dbo.BuyPlanGoods where dealstatus=1";
                break;
            case 2:
                strsele = "select count(distinct BuyPlanId) as num from dbo.BuyPlanGoods where dealstatus=2";
                break;
            case 4:
                strsele = "select count(distinct BuyPlanId) as num from dbo.BuyPlanGoods where dealstatus=4";
                break;
            case 5:
                strsele = "select count(distinct BuyPlanId) as num from dbo.BuyPlanGoods where dealstatus=5";
                break;
            case 6:
                strsele = "select count(distinct BuyPlanId) as num from dbo.BuyPlanGoods where dealstatus=2 and datediff(dd,HospitalInflowDate,getdate())>60";
                break; 
        }
        Db db = new Db();
        DataTable dt = db.DataTable(strsele);
        db.Close();
        switch (i)
        {
            case 1:
                Label7.Text = dt.Rows[0].ItemArray[0].ToString();
                break;
            case 2:
                Label6.Text = dt.Rows[0].ItemArray[0].ToString();
                break;
            case 4:
                Label5.Text = dt.Rows[0].ItemArray[0].ToString();
                break;
            case 5:
                Label4.Text = dt.Rows[0].ItemArray[0].ToString();
                break;
            case 6:
                Label3.Text = dt.Rows[0].ItemArray[0].ToString();
                break;
        }
    }

    private void HosptialTotal()//医院统计
    {
        string strtt = @"select b.areaname,c.yy,c.yytt from 
                    (
                    select  distinct a.areaid,sum(a.h) as yytt,sum(a.tt) as yy
                    from 
                     (
                    select areaid,0 as h,count(areaid) as tt 
                      from hospital group by areaid
                    UNION  
                    select areaid,count(distinct AccountId_YY) as yy,0 as j  from Tra_Goodsid_200708 group by areaid
                     ) as a    group by a.areaid
                    ) as c ,area as b where c.areaid=b.areaid";
        Db db = new Db();
        DataTable dt = db.DataTable(strtt);
        db.Close();
        GridView1.DataSource = dt;
        GridView1.DataBind();
    }
    protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
    {
        if (e.Row.RowType == DataControlRowType.DataRow)
        {
           yy += Convert.ToInt32(e.Row.Cells[1].Text.Trim());
           yytt += Convert.ToInt32(e.Row.Cells[2].Text.Trim());
        }
        if (e.Row.RowType== DataControlRowType.Footer)
        {
            e.Row.Cells[0].Text = "总计：";
            e.Row.Cells[0].HorizontalAlign = HorizontalAlign.Center;
            e.Row.Cells[1].Text =yy.ToString() ;
            e.Row.Cells[1].HorizontalAlign = HorizontalAlign.Center;
            e.Row.Cells[2].Text = yytt.ToString();
            e.Row.Cells[2].HorizontalAlign = HorizontalAlign.Center;
        }

    }
}
